/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.se2.meteocal.gui;

import it.polimi.se2.meteocal.control.UserManager;
import it.polimi.se2.meteocal.entity.Users;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Ugo
 */
@ManagedBean
@SessionScoped
public class UserBean {

    @EJB
    UserManager userManager;
    
    private Users user;
    private String password1;
  
    public UserBean() {
       
    }
    
    
    @PostConstruct
    public void init(){
        user=userManager.getLoggedUser();
        password1=user.getPassword();
    }
     
    public Users getUser(){
        return this.user;
    }
    
    public void setUser(Users user){
        this.user=user;
    }
    
    public String getPassword1(){
        return this.password1;
    }
    
    public void setPassword1(String password1){
        this.password1=password1;
    }
    
   
    public String getName() {
        return userManager.getLoggedUser().getName();
    }
    
    public String getEmail() {
        return userManager.getLoggedUser().getEmail();
    }
    
    public String getSurname() {
        return userManager.getLoggedUser().getSurname();
    }
    
    public String getCity() {
        return userManager.getLoggedUser().getCity();
    }
    
    public String getPassword() {
        return userManager.getLoggedUser().getPassword();
    }
     
     public void modifyUser(){
        FacesContext context = FacesContext.getCurrentInstance(); 
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"", "Update completed"));
        if(password1!=null)
            user.setPassword(password1);
        userManager.edit(user);
    }
     
}